<template>
  <el-dialog v-model="visible" :title="title" width="1000" show-footer show-zoom resize :close-on-click-modal="false"
    :close-on-press-escape="false" @close="close">
    <template #default>
      <vxe-table ref="tableRef" :data="form.detail" :round="true" height="300" @checkbox-all="selectAllChangeEvent"
        @checkbox-change="selectChangeEvent">
        <vxe-column type="checkbox" width="60"></vxe-column>
        <vxe-column title="箱号">
          <template #default="{ row }" width="120">
            <el-input v-model="row.head_box_order_no" placeholder="输入箱号" :disabled="handleType == 'view'" />
          </template>
        </vxe-column>
        <vxe-column field="shipment_id" title="Shipment ID">
          <template #default="{ row }">
            {{ row.shipment_id || '--' }}
          </template>
        </vxe-column>
        <vxe-column field="to_fn_sku" title="FNSKU">
          <template #default="{ row }">
            {{ row.to_fn_sku || '--' }}
          </template>
        </vxe-column>
        <vxe-column field="sku" title="SKU">
          <template #default="{ row }">
            {{ row.sku || '--' }}
          </template>
        </vxe-column>
        <vxe-column title="图片">
          <template #default="{ row }">
            <div>
              <div class="image-item" v-for="(item, index) in row.image" :key="index"><img :src="item"></div>
            </div>
          </template>
        </vxe-column>
        <vxe-column title="名称" width="250">
          <template #default="{ row }">
            {{ row.sku_cn_title }}
          </template>
        </vxe-column>
        <vxe-column title="申请数量">
          <template #default="{ row }">
            {{ row.apply_num }}
          </template>
        </vxe-column>
        <vxe-column field="get_goods_num" title="收货数量">
          <template #default="{ row }">
            <el-input v-model="row.get_goods_num" placeholder="收货数量" :disabled="handleType == 'view'"/>
          </template>
        </vxe-column>
      </vxe-table>
    </template>

    <template #footer>
      <template v-if="handleType == 'view'">
        <div class="flex justify-center py-2">
          <el-button type="primary" @click="close">关 闭</el-button>
        </div>
      </template>
      <div class="flex justify-center py-2" v-else>
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="handleReceiving">收货</el-button>
      </div>

    </template>
  </el-dialog>
</template>
<script setup name="ComModal" lang="jsx">
import { getHeadOrderDetail, receivingHeadOrder } from '../api/index'
import { reactive, ref, toRefs } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const visible = ref(false)
const emits = defineEmits(['query'])

const props = defineProps({
  wareDataList: {
    type: Array,
    default: [],
  },
})

const data = reactive({
  form: {},
  title: '新增',
  handleType: null,
  records: []
})
const { form, title, handleType, records } = toRefs(data)

const statusMap = new Map([
  ['add', '新增'],
  ['edit', '编辑'],
  ['view', '查看'],
  ['receiving', '收货'],
])

const open = (type, id) => {
  title.value = statusMap.get(type)
  handleType.value = type
  visible.value = true
  if (id) getDetail(id)
}

const getDetail = async (id) => {
  try {
    const res = await getHeadOrderDetail({
      id
    })
    form.value = res.data
  } catch (e) {
    throw Error(e)
  }
}

const tableRef = ref()
const selectChangeEvent = (rows,checked) => {
  const $table = tableRef.value
  if ($table) {
    $table.setCheckboxRow(rows, checked)
  }
}

const selectAllChangeEvent = () => {
  const $table = tableRef.value
  if ($table) {
    $table.setAllCheckboxRow(true)
  }
}

// 收货
const handleReceiving = async () => {
  const $table = tableRef.value
  const selectRecords = $table.getCheckboxRecords()
  if(selectRecords.length == 0){
    ElMessage.error('至少选择一条')
    return
  }
  const massageArray = []

  const detail = selectRecords.map((i,index) => {
    if (!i.get_goods_num) {
      massageArray.push(`第 ${index + 1} 条收货数量需要大于0`)
    }
    return {
      id: i.id,//头程退货单明细id
      head_box_order_no: i.head_box_order_no || '',//箱号
      get_goods_num: i.get_goods_num//收货数量
    }
  })
  if (massageArray && massageArray.length) {
    return ElMessageBox({
      title: '提示',
      message: <div>
        {massageArray.map(d => h('p', null, d))}
      </div>,
    })
  }
  try {
    const res = await receivingHeadOrder({
      id: form.value.id,
      detail
    })
    ElMessage.success('操作成功')
    close()
  } catch (e) {
    throw Error(e)
  }
}

const close = () => {
  visible.value = false
}
defineExpose({ open })
</script>
<style lang="scss" scoped>
.borderNone {
  :deep(.el-input__wrapper) {
    border: none;
    box-shadow: none
  }
}
</style>
